package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NewsDao;
import com.cn.wanxi.dao.impl.NewsImpl;
import com.cn.wanxi.dto.NewsModelDto;
import com.cn.wanxi.dto.ResultModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.INewsService;

import java.util.List;

public class NewsServiceImpl implements INewsService {
    /**
     * 得到三种信息
     * 1.公司信息
     * 2.导航信息
     * 3.新闻信息
     *
     * @return 分页数据还有总条数
     */
    @Override
    public NewsModelDto getNewsModel(PageModel pageModel) {
        NewsModelDto newsModel = new NewsModelDto();
        //得到新闻数据
        newsModel.setNewsModelList(getNewsModelList(pageModel));
        //得到总条数
        newsModel.setPageCount(getNewsCount());
        //得到页面信息
        newsModel.setPageModel(pageModel);
        return newsModel;
    }

    @Override
    public Integer getNewsCount() {
        NewsDao newsDao = new NewsImpl();
        return newsDao.getNewsCount();
    }

    /**
     * 这里返回的是一个结果，而不是Integer
     * 因为我们不能再控制层去做逻辑管理，
     * 只能在这里去做
     *
     * @param newsModel
     * @return
     */
    @Override
    public ResultModel addNews(NewsModel newsModel) {
        NewsDao newsDao = new NewsImpl();
        Integer result = newsDao.addNews(newsModel);
        ResultModel resultModel = new ResultModel();
        if (result == 1) {
            resultModel.setCode("200");
            resultModel.setResult("成功");
//        resultModel.setList(null);
        } else {
            resultModel.setCode("400");
            resultModel.setResult("失败");
//        resultModel.setList(null);
        }
        return resultModel;
    }

    @Override
    public NewsModel findById(NewsModel newsModel) {
        NewsDao newsDao = new NewsImpl();

        return newsDao.findById(newsModel);
    }

    @Override
    public ResultModel findNewsBySome(NewsModel newsModel) {
        NewsDao newsDao = new NewsImpl();
        return new ResultModel(newsDao.findNewsBySome(newsModel));
    }

    /**
     * 得到新闻数据集合
     *
     * @return
     */
    public List<NewsModel> getNewsModelList(PageModel pageModel) {
        NewsDao newsDao = new NewsImpl();
        return newsDao.getNewsModelList(pageModel);
    }


    @Override
    public ResultModel add(Object o) {
        return null;
    }

    @Override
    public ResultModel update(Object o) {
        return null;
    }

    @Override
    public ResultModel del(Object o) {
        return null;
    }

    @Override
    public ResultModel findAll(Object o) {
        return null;
    }

    @Override
    public ResultModel findByName(Object o) {
        return null;
    }

    @Override
    public ResultModel findById(Object o) {
        return null;
    }

    @Override
    public ResultModel test(Object o) {
        NewsDao newsDao = new NewsImpl();
        return new ResultModel(newsDao.findNewsBySome((NewsModel) o));
    }
}
